package cn.hotel.controller.worker;

import cn.hotel.entity.OrderType;
import cn.hotel.response.ResponseTool;
import cn.hotel.response.Result;
import cn.hotel.service.OrderTypeService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/op/order-type")
public class OpOrderTypeController {
    @Autowired
    private OrderTypeService orderTypeService;

    /**
     * 分页查询
     * 获取所有的预定方式
     * @return
     */
    @PostMapping("/list")
    public Result getALLOrderType(Integer page , Integer pageSize){
        PageInfo<OrderType> pageInfo = orderTypeService.queryOrderTypeList(page, pageSize);
        return ResponseTool.success(pageInfo);
    }

    /**
     * 添加预定方式
     */
    @PostMapping("/add")
    public Result addOrderType(OrderType orderType){
        orderTypeService.insertOrderType(orderType);
        return ResponseTool.success("添加预定方式成功");
    }

    /**
     * 通过预定方式的id获取预定方式信息
     */
    @PostMapping("/{orderTypeId}")
    public Result getOrderTypeById(@PathVariable("orderTypeId") int orderTypeId){
        log.info("orderTypeId:{}",orderTypeId);
        OrderType orderType = orderTypeService.queryOrderTypeById(orderTypeId);
        return ResponseTool.success(orderType);
    }

    /**
     * 更新预定方式
     */
    @PostMapping("/update")
    public Result changeOrderType(OrderType orderType){
        log.info("orderType:{}",orderType);
        orderTypeService.updateOrderType(orderType);
        return ResponseTool.success("更新成功");
    }

    /**
     * 删除预定方式
     */
    @DeleteMapping("/delete")
    public Result removeOrderType(@RequestParam("orderTypeIds") List<Integer> orderTypeIds){
        log.info("orderTypeIds:{}",orderTypeIds);
        for (Integer orderTypeId : orderTypeIds) {
            orderTypeService.deleteOrderType(orderTypeId);
        }
        return ResponseTool.success("删除信息成功");
    }

    @PostMapping("")
    public Result getAllOrder() {
        List<OrderType> all = orderTypeService.getAll();
        if (all == null) {
            return ResponseTool.failed("查询失败");
        }
        return ResponseTool.success(all);
    }

}
